{% import _self as self %}
{% if grade_inquiry_per_component_allowed %}
<h3>Grade Inquiries</h3>
{% endif %}
<p>{{ regrade_message }}</p>
{% if not has_submission %}
    <span class="yellow-message">No Submission</span>
{% endif %}
{% if can_inquiry and has_submission %}
    {% if grade_inquiry_per_component_allowed %}
        {%  include "submission/regrade/ComponentTabs.twig" with { "gradeable_components": gradeable_components, 'grade_inquiries': grade_inquiries } only %}
    {% endif %}
    <div class='grade-inquiries'>
        {% for component in gradeable_components %}
            <div class="grade-inquiry" data-component_id={{ component.id }}>
                <div class="grade-inquiry-header-div">
                    {% if (grade_inquiry_per_component_allowed and (not component.id == 0 or grade_inquiries[component.id].status == -1)) or not grade_inquiry_per_component_allowed %}
                        <h3 class="grade-inquiry-header">Grade Inquiry:</h3>
                        <span class="grade-inquiry-status">
                        {% if grade_inquiries[component.id].status == -1 and is_grading %}
                                <span class="fas fa-hourglass" style="font-size: 16px; color: #af0000"></span>
                                <span class="red-message">Unresolved Grade Inquiry</span>
                        {% elseif grade_inquiries[component.id].status == -1 %}
                            <span class="fas fa-hourglass" style="font-size: 16px; color: #af0000"></span>
                            <span class="red-message">Grade Inquiry Under Review</span>
                        {% elseif grade_inquiries[component.id] is defined and grade_inquiries[component.id].status is not null and grade_inquiries[component.id].status == 0 %}
                            <span class="fas fa-check" style="font-size: 16px; color: #008800"></span>
                            <span class="green-message">Grade Inquiry Has Been Resolved</span>
                        {% endif %}
                        </span>
                    {% endif %}
                </div>
                {% if not grade_inquiries[component.id].posts is empty %}
                    {% for post in grade_inquiries[component.id].posts %}
                        <div class='{{ post.is_staff ? "post_box important" : "post_box" }}'>
                            <p style='white-space:pre-wrap;'>{% include "forum/RenderPost.twig" with { "post_content": post.content } only %}</p>
                            <hr>
                            <div style="float:left">
                                {% if grade_inquiry_per_component_allowed and component.id == 0 %}
                                    <p class="component-identifier-post"><b>{{ post.gc_title }}</b></p>
                                {% endif %}
                            </div>
                            <div style="float:right">
                                <b>{{ post.name }}</b> &nbsp;
                                {{ post.date }}
                            </div>
                        </div>
                    {% endfor %}
                {% elseif component.id == 0 %}
                    <p class="yellow-message no-post-warning" >No posts!</p>
                {% endif %}
                {% if (grade_inquiry_per_component_allowed and (not component.id == 0 or grade_inquiries[component.id].status == -1)) or not grade_inquiry_per_component_allowed %}
                    <form method="POST" class="reply-text-form" id="reply-text-form-{{ component.id }}" >
                        <input type="hidden" name="csrf_token" value="{{ csrf_token }}" >
                        <input type="hidden" id="thread_id" name="thread_id" value="{{ grade_inquiries[component.id].id is defined ? grade_inquiries[component.id].id : 0  }}">
                        <input type="hidden" id="submitter_id" name="submitter_id" value="{{ submitter_id }}">
                        <input type="hidden" id="gc_id" name="gc_id" value="{{ component.id }}">
                        {% if grade_inquiries[component.id] is not defined or grade_inquiries[component.id].status == 0 %}
                            {% set text_area_label_text = 'Write an inquiry:' %}
                        {% elseif grade_inquiries[component.id] == -1 %}
                            {% set text_area_label_text = 'Add comment to grade inquiry:' %}
                        {% endif %}
                        <label for="reply-text-area">{{ text_area_label_text }}</label>
                        <textarea name="replyTextArea" id="reply-text-area-{{ component.id }}" style="resize:none;min-height:100px;width:100%; font-family: inherit;" rows="10" cols="30" placeholder="{{ "Type Response Here" }}" required onkeyup="onReplyTextAreaKeyUp(this)"></textarea>
                        <div class="row" style="justify-content: flex-end">
                        <div style="margin-right: auto">
                        {{ self.markdown_buttons() }}
                        </div>
                            {% if grade_inquiries[component.id] is not defined or grade_inquiries[component.id].status is null %}
                                {# No request yet #}
                                <button disabled type="button" formaction="{{ request_regrade_url }}" class="gi-submit btn btn-primary" style="float: right" onclick="onGradeInquirySubmitClicked(this)">
                                    Submit Grade Inquiry
                                </button>
                            {% elseif grade_inquiries[component.id].status == -1 and is_grading %}
                                {# Grader view,request is open #}
                                <button disabled type="button" formaction="{{ change_request_status_url }}" class="gi-submit btn btn-primary" style="margin:15px 0 0 5px; float: right" onclick="onGradeInquirySubmitClicked(this)">
                                    Respond and Resolve Grade Inquiry
                                </button>
                                <button disabled type="button" formaction="{{ make_request_post_url }}" class="gi-submit btn btn-primary" style="margin:15px 0 0 5px; float: right" onclick="onGradeInquirySubmitClicked(this)">
                                    Post Additional Information
                                </button>
                                <button type="button" id="grading-close gi-ignore-disabled" formaction="{{ change_request_status_url }}" class="gi-submit btn btn-default" style="margin:15px 0 0 5px; float: right" onclick="onGradeInquirySubmitClicked(this)">
                                    Close Grade Inquiry
                                </button>
                            {% elseif grade_inquiries[component.id].status == -1 %}
                                {# Request pending, we can submit new posts #}
                                <button disabled type="button" formaction="{{ make_request_post_url }}" class="gi-submit btn btn-primary" style="margin:15px 0 0 5px; float: right" onclick="onGradeInquirySubmitClicked(this)">
                                    Post Additional Information
                                </button>
                                <button disabled type="button" formaction="{{ change_request_status_url }}" class="gi-submit btn btn-default" style="margin:15px 0 0 5px; float: right" onclick="onGradeInquirySubmitClicked(this)">
                                    Close Grade Inquiry
                                </button>
                            {% elseif grade_inquiries[component.id].status == 0 and is_grading %}
                                {# Grader view, Request closed #}
                                <button type="button" id="gi-ignore-disabled" formaction="{{ change_request_status_url }}" class="gi-submit btn btn-primary" style="margin:15px 0 0 5px; float: right" onclick="onGradeInquirySubmitClicked(this)">
                                    Reopen Grade Inquiry
                                </button>
                            {% elseif grade_inquiries[component.id].status == 0 %}
                                {# Request closed #}
                                <button disabled type="button" formaction="{{ change_request_status_url }}" class="gi-submit btn btn-default" style="margin:15px 0 0 5px; float: right" onclick="onGradeInquirySubmitClicked(this)">
                                    Reopen Grade Inquiry
                                </button>
                            {% endif %}
                        </div>
                    </form>
                {% endif %}
            </div>
        {% endfor %}
    </div>
{% endif %}

{% macro markdown_buttons() %}
    <button type="button" title="Insert a link" onclick="addMarkdownCode(1, '#reply-text-area')" style="margin-right:10px;" class="btn btn-default">
        Link <i class="fas fa-link fa-1x"></i>
    </button>
    <button title="Insert a code segment" type="button" onclick="addMarkdownCode(0, '#reply-text-area')" class="btn btn-default">
        Code <i class="fas fa-code fa-1x"></i>
    </button>
{% endmacro %}
